package com.nf.health.app.fragment;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.PraiseAdapter;
import com.nf.health.app.models.Praise;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

public class PraiseSuccessFragment extends IBaseFragment {

	private ListView listview;
	private List<Praise> praises;
	private PraiseAdapter praiseAdapter;
	private int pos;
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		addViewFillInRoot(R.layout.fragment_praise);
		listview = ViewUtil.getView(rootView, R.id.listview);
		initData();
		
		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					final int position, long id) {
				if(praises!= null){
					pos = position - 1;
					int state = Integer.parseInt(praises.get(pos).getIs_accept());
					switch (state) {
					case Praise.STATE_RECEIVE:
						
						break;
					case Praise.STATE_FINISH:
						
						break;
					case Praise.STATE_GIVE_UP:
						
						break;
					case Praise.STATE_NO_RECEIVE:
					case Praise.STATE_NO_PAY:
					case Praise.STATE_REFUND:
						Builder builder = new AlertDialog.Builder(getActivity());
						builder.setPositiveButton("确定", new OnClickListener() {
							

							@Override
							public void onClick(DialogInterface dialog, int which) {
								provider.requestDelParise(praises.get(pos).getMajor_id(),"delPraise");
							}
						});
						builder.setNegativeButton("取消", null);
						builder.setTitle("提示").setMessage("确定要删除么？").create().show();
						break;
					default:
						break;
					}
				}
				return false;
			}
		});
	}
	
	private void initData() {
		provider.requestPraise("success", "praise");
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		if("praise".equals(actionName)){
			praises = (List<Praise>)object;
			if(praises.size()<=0){
				showNoData();
			}else{
				praiseAdapter = new PraiseAdapter(getActivity(),praises);
				listview.setAdapter(praiseAdapter);
			}
		}else{
			showToast("删除成功！");
//			initData();
			praises.remove(pos);
			if(praiseAdapter != null){
				praiseAdapter.notifyDataSetChanged();
			}
		}
	}
}
